package com.affectu.task.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.model.SelectItem;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.component.html.HtmlTab;
import org.richfaces.component.html.HtmlTabPanel;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

import com.affectu.category.Category;
import com.affectu.dao.DAOBase;
import com.affectu.task.InnerTask;
import com.affectu.user.User;

/*
 * 2008-9-2
 * @author daniel nathan
 */
@Name("taskAddAction")
public class Add extends DAOBase {

	@In(required = false)
	private InnerTask innerTask;
	public HtmlTab htmlTabFirst;
	public HtmlTab htmlTabSecond;
	public HtmlTab htmlTabThird;
	public HtmlTabPanel htmlTabPanel;
	private HtmlPanelGrid htmlGrid;
	private String attachName;

	public void openResgisterPanel() {
		this.getHtmlGrid().setRendered(true);
	}

	public String register(User user) {
		try {
			em.persist(user);
			this.setCurrentUserToSession(user);
			innerTask.setOwner((this.getCurrentUser()));// 把登陆用户默认为任务拥有人
		} catch (Exception ex) {
			this.logException(ex);
		}
		// this.getHtmlGrid().setRendered(false);
		return "";
	}

	@Begin(join = true)
	public String firstNext() {
		try {
			this.getHtmlTabFirst().setDisabled(true);
			this.getHtmlTabSecond().setDisabled(false);
			this.getHtmlTabThird().setDisabled(true);
			this.getHtmlTabPanel().setSelectedTab(
					this.getHtmlTabSecond().getName());
			innerTask.setOwner((this.getCurrentUser()));// 把登陆用户默认为任务拥有人
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "";
	}

	public String secondNext() {
		try {
			this.getHtmlTabFirst().setDisabled(true);
			this.getHtmlTabSecond().setDisabled(true);
			this.getHtmlTabThird().setDisabled(false);
			this.getHtmlTabPanel().setSelectedTab(
					this.getHtmlTabThird().getName());
			innerTask.setOwner((this.getCurrentUser()));// 把登陆用户默认为任务拥有人
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "";
	}

	@End(beforeRedirect = true)
	public String add() {
		try {
			// Task task = (Task) this.getSessionScope().get("task");
			innerTask.setPublishDate(new Date());// 设置任务发布时间
			innerTask.setOwner((this.getCurrentUser()));// 把登陆用户默认为任务拥有人
			innerTask.setAttachName(this.getSessionScope().get("attachName")
					+ "");//
			em.save(innerTask);
			// this.getSessionScope().remove("task");
			this.getSessionScope().remove("attachName");
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "/passport/index.xhtml";
	}

	public String modify(InnerTask task) {
		try {
			em.merge(task);
			em.flush();
			this.log("task.name:" + task.getName());
			this.log("ok;");
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "/passport/index.xhtml";
	}

	public void delete(InnerTask task) {
		try {
			em.delete(task);
		} catch (Exception ex) {
			this.logException(ex);
		}
	}

	public List<SelectItem> getDaysItems() {
		List<SelectItem> days = new ArrayList<SelectItem>();
		for (int i = 1; i < 46; i++) {
			days.add(new SelectItem(i, i + "天"));
		}
		return days;
	}

	public String addCategoriesToTag(Category category) {
		if ("".equals(this.innerTask.getTag())) {
			this.innerTask.setTag(category.getName());
		} else {
			this.innerTask.setTag(this.innerTask.getTag() + ","
					+ category.getName());
		}
		return "";
	}

	public HtmlTab getHtmlTabFirst() {
		return htmlTabFirst;
	}

	public void setHtmlTabFirst(HtmlTab htmlTabFirst) {
		this.htmlTabFirst = htmlTabFirst;
	}

	public HtmlTab getHtmlTabSecond() {
		return htmlTabSecond;
	}

	public void setHtmlTabSecond(HtmlTab htmlTabSecond) {
		this.htmlTabSecond = htmlTabSecond;
	}

	public HtmlTab getHtmlTabThird() {
		return htmlTabThird;
	}

	public void setHtmlTabThird(HtmlTab htmlTabThird) {
		this.htmlTabThird = htmlTabThird;
	}

	public HtmlTabPanel getHtmlTabPanel() {
		return htmlTabPanel;
	}

	public void setHtmlTabPanel(HtmlTabPanel htmlTabPanel) {
		this.htmlTabPanel = htmlTabPanel;
	}

	public HtmlPanelGrid getHtmlGrid() {
		return htmlGrid;
	}

	public void setHtmlGrid(HtmlPanelGrid htmlGrid) {
		this.htmlGrid = htmlGrid;
	}

	public String getAttachName() {
		return attachName;
	}

	public void setAttachName(String attachName) {
		this.attachName = attachName;
	}

	@Begin(join = true)
	public void uploadListener(UploadEvent event) {
		try {
			UploadItem item = event.getUploadItem();
			String fileName = item.getFileName();
			fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);

			Random rand = new Random();
			this.attachName = rand.nextInt(999) + fileName;// 防止同名附件产生
			this.getSessionScope().put("attachName", this.attachName);//
			String filepath = this.getRealPath("/") + "upload/task/";
			File file = new File(filepath, this.attachName);
			FileInputStream fis = new FileInputStream(item.getFile());
			FileOutputStream out = new FileOutputStream(file);
			int bytes = 0;
			byte[] bteFile = new byte[1024];
			while ((bytes = fis.read(bteFile)) != -1) {
				out.write(bteFile, 0, bytes);
			}
			out.flush();
			out.close();
			FacesMessages.instance()
					.add("文件上传成功！" + filepath + this.attachName);
		} catch (Exception ex) {
			FacesMessages.instance().add("文件上传失败！");
			this.logException(ex);
		}
	}

	public InnerTask getTask() {
		return innerTask;
	}

	public void setTask(InnerTask task) {
		this.innerTask = task;
	}
}
